package com.erification.web.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.erification.web.dao.UserDao;
import com.erification.web.model.User;
@Transactional @Component("userDao")
public class UserDaoImpl implements UserDao {
	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public void save(User user) {
		sessionFactory.getCurrentSession().persist(user);
	}

	@Override
	public void delete(User user) {
		sessionFactory.getCurrentSession().delete(user);
	}

	@Override
	public void update(User user) {
		sessionFactory.getCurrentSession().update(user);
	}

	@Override @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public User getUser(String username) {
		String queryString = "From User where username=:username";
		Query query = sessionFactory.getCurrentSession().createQuery(queryString);
		query.setString("username", username);
		return (User) query.uniqueResult();
	}

}
